import { LinValidator, Rule } from "../code/lin-validator-v2"
import User from '../sql/module/user/user'
import { logtype } from "../lib/enum";
export class UsersValidator extends LinValidator {
    constructor() {
        super()
        this.limit = [new Rule('isLength', '一页数据不超100条', {
            max: 2
        })]
        this.page_index = []
        this.RoomNumberId = [new Rule('isInt', '区域错误')]
    }
}
export class TokenValidator extends LinValidator {
    constructor() {
        super()
        this.phone = [
            new Rule('isLength', '账号长度为4-16', {
                min: 4,
                max: 16
            })
        ]
        this.password = [
            new Rule('isLength', '至少五位', {
                min: 5,
                max: 32
            })
        ]
    }
    validateLoginType(vals: { body: { type: any } }) {
        const type = vals.body.type
        if (!type) {
            throw new Error('type为必填参数')
        }
        if (!logtype.has(type)) {
            throw new Error('type参数不合法')
        }
    }
}

export class RegisterValidator extends LinValidator {
    constructor() {
        super()
        this.phone = [
            new Rule('isLength', '账号长度为4-16', {
                min: 4,
                max: 16
            })
        ]
        this.nickname = [
            new Rule('isLength', '昵称长度不符合规则', {
                min: 2,
                max: 16
            })
        ]
    }
    async validateEmail(vals: { body: { phone: any } }) {
        const phone = vals.body.phone

        const n = await User.findOne({
            where: {
                phone
            }
        })
        if (n) {
            throw new Error('当前账号已存在')
        }
    }
}
export class IntValidator extends LinValidator {
    constructor() {
        super()
        this.key = [
            new Rule('isLength', '长度不符合', {
                min: 1,
                max: 5
            })
        ]
    }
}
export class ChangePassSelfValidator extends LinValidator {
    constructor() {
        super()
        this.password = [
            new Rule('isLength', '至少五位', {
                min: 5,
                max: 32
            })
        ]
        this.password1 = [
            new Rule('isLength', '密码至少6位，最多32位', {
                min: 6,
                max: 32
            }),
            // new Rule('matches', '密码不符合规范', '^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$')
        ]
    }
    validatePassword(vals: { body: { password1: any; password2: any } }) {
        const psw1 = vals.body?.password1
        const psw2 = vals.body?.password2
        if (psw1 !== psw2) {
            throw new Error('两个密码必须相同')
        }
    }
}
export class ChangeInfoValidator extends LinValidator {
    constructor() {
        super()
        this.id = []
        this.phone = [
            new Rule('isOptional'),
            new Rule('isLength', '账号长度为4-16', {
                min: 4,
                max: 16
            })
        ]
        this.nickname = [
            new Rule('isOptional'),
            new Rule('isLength', '昵称长度不符合规则', {
                min: 2,
                max: 16
            })
        ]
    }
    async validatePhone(vals: { body: { id: any; phone: any }; query: { id: any } }) {
        const id = vals.body?.id || vals.query?.id
        const phone = vals.body?.phone
        if (!phone) return
        const user = await User.findOne({
            where: {
                id
            }
        })
        if (user?.dataValues.phone == phone) return
        const n = await User.findOne({
            where: {
                phone
            }
        })
        if (n) {
            throw new Error('当前账号已存在')
        }
    }
}
export class ChangeSelfInfoValidator extends LinValidator {
    constructor() {
        super()
        this.phone = [
            new Rule('isOptional'),
            new Rule('isLength', '账号长度为4-16', {
                min: 4,
                max: 16
            })
        ]
        this.nickname = [
            new Rule('isOptional'),
            new Rule('isLength', '昵称长度不符合规则', {
                min: 2,
                max: 16
            })
        ]
    }
    async validateEmail(vals: { body: { id: any; phone: any } }) {
        const id = vals.body?.id
        const phone = vals.body?.phone

        const u = await User.findOne({
            where: {
                id
            }
        })
        if (u?.dataValues.phone == phone) return
        const n = await User.findOne({
            where: {
                phone
            }
        })
        if (n) {
            throw new Error('当前账号已存在')
        }
    }
}
export class DeleteValidator extends LinValidator {
    constructor() {
        super()
    }
}
export class RoomNumbersValidator extends LinValidator {
    constructor() {
        super()
    }
}
export class PageIndexValidator extends LinValidator {
    constructor() {
        super()
        this.url = []
    }
}

export class SubMenusValidator extends LinValidator {
    constructor() {
        super()
        this.upLevel = []
    }
}